//
//  UILabel+JJExtension.swift
//  JJToolSwift
//
//  Created by heihei on 2024/6/6.
//

import Foundation

public extension UILabel {
    /// 给label添加attributedtext
    /// - Parameters:
    ///   - text: 文本内容
    ///   - attriString: 需要特殊设置颜色的文字
    ///   - color: 设置的颜色
    func addAttributedText(text: String?, attriString: String, color: UIColor) {
        self.text = text
        guard let tempText = text else { return }
        guard let ranges = tempText.rangesFromString(string: attriString) else { return }
        let mutableAttribute = NSMutableAttributedString(string: tempText)
        ranges.forEach { range in
            let nsrange = tempText.nsRange(from: range)
            mutableAttribute.addAttributes([.foregroundColor: color], range: nsrange)
        }
        self.attributedText = mutableAttribute
    }
}
